pub fn encode(source: &str) -> String {
    let mut tmp = '0';
    let mut cnt = 0;
    let mut result = String::new();
    for ch in source.chars() {
        if tmp != ch {
            if cnt > 1 {
                result.push_str(&cnt.to_string());
                result.push(tmp);
                tmp = ch;
                cnt = 1;
            } else if cnt == 1 {
                result.push(tmp);
                tmp = ch;
                cnt = 1;
            } else {
                cnt += 1;
                tmp = ch;
            }
        } else {
            cnt += 1;
        }
    }
    if cnt > 1 {
        result.push_str(&cnt.to_string());
        result.push(tmp);
    } else if cnt == 1 {
        result.push(tmp);
    }
    result
}

pub fn decode(source: &str) -> String {
    let enumerate = source.chars().collect::<Vec<char>>();
    let mut result = String::new();
    let mut cnt = String::new();
    for i in 0..enumerate.len() {
        if let Some(&c) = enumerate.get(i) {
            if c.is_numeric() {
                cnt.push(c);
            } else {
                if !cnt.is_empty() {
                    let mut num = cnt.parse::<u32>().unwrap();
                    if num != 0 {
                        while num != 0 {
                            result.push(c);
                            num -= 1;
                        }
                        cnt.clear();
                    } else {
                        result.push(c);
                    }
                } else {
                    result.push(c);
                }
            }
        }
    }
    result
}


#[test]
#[ignore]
fn encode_single_characters_only_are_encoded_without_count() {
    let input = "XYZ";
    let output = encode(input);
    let expected = "XYZ";
    assert_eq!(output, expected);
}